var authURL = "src/services/MaskerService.php?action=checkMaskerAuth";
var anonymousAuthURL = "src/services/MaskerService.php?action=anonymousMaskerAuth";
var existURL = "src/services/MaskerService.php?action=checkMaskerExist";
var registerURL = "src/services/MaskerService.php?action=registerMasker";
function login() {
	openLoading();
	$("#login").html(getLoaderTransparent());
	var user = $('#masker').val();
	var psw = $('#password').val();
	var errors = "";

	if (user == "") {
		errors += "<li>" + getMessage("login.user.empty") + "</li>";
	}
	
	if (psw == "") {
		errors += "<li>" + getMessage("login.psw.empty") + "</li>";
	}

	if (errors == "" && auth(user, psw) != "OK") {
		errors += "<li>" + getMessage("login.user.or.psw.invalid") + "</li>";
	}
	
	
	if (errors != "") {
		errors = "<ul>" + errors + "</ul>";
		document.getElementById("errorMessage").innerHTML = errors;
		closeLoading();
		$("#login").html(getMessage("index.login"));
		$('#loginError').popup('open');
	} else {
		closeLoading();
		$("#login").html(getMessage("index.login"));
		// if auth ok, go to home page
		window.location.href = "home.php#watchers";
	}
}
function redirectToAuthor() {
	window.open("http://quarantaduelab.altervista.org/",'_blank');
}
function loginAnonymous() {
	openLoading();
	getJSON($.ajax({
		type : "POST",
		async : false,
		url : anonymousAuthURL,
		dataType : "JSON"
	})).result;
	window.location.href = "home.php#global";
	closeLoading();
}

function register() {
	openLoading();
	$("#confirmRegister").html(getLoaderTransparent());
	
	var user = $('#reg_user').val();
	var psw = $('#reg_password').val();
	var repsw = $('#reg_retypepassword').val();
	var mail = $('#reg_mail').val();

	var errors = "";

	if (user == "") {
		errors += "<li>" + getMessage("register.user.empty") + "</li>";
	} else if (user.length < 3) {
		errors += "<li>" + getMessage("register.user.at.least.3") + "</li>";
	} else if (hasWhiteSpace(user)) {
		errors += "<li>" + getMessage("login.user.no.white.spaces") + "</li>";
	}
	
	if (mail == "") {
		errors += "<li>" + getMessage("register.mail.empty") + "</li>";
	} else if(!validateEmail(mail)) {
		errors += "<li>" + getMessage("register.mail.no.valid") + "</li>";
	}
	
	if (repsw == "") {
		errors += "<li>" + getMessage("register.re.psw.empty") + "</li>";
	} else if (psw != repsw) {
		errors += "<li>" + getMessage("register.psw.and.re.psw.not.equal") + "</li>";
	}

	if (user != "" && exist(user)) {
		errors += "<li>" + getMessage("register.user.already.exist") + "</li>";
	}

	if (errors != "") {
		closeLoading();
		$("#confirmRegister").html(getMessage("register.confirm"));
		openErrorPopUp(errors);
	} else {
		// register
		var result = insert(user, psw, mail);
		closeLoading();
		$("#confirmRegister").html(getMessage("register.confirm"));
		if (!result) {
			openErrorPopUp(getMessage("register.impossible.retry"));
		}
		var notifications = getMessage("register.welcome.masker") + " <b>" + user + "</b><br>";
		notifications += getMessage("register.welcome.masker.second.part") + "<br><br>";
		notifications += getMessage("register.welcome.masker.third.part");
		document.getElementById("notificationMessage").innerHTML = notifications;
		$('#registerNotification').popup('open');
	}

}

function auth(user, psw) {
	return getJSON($.ajax({
		type : "POST",
		async : false,
		url : authURL,
		data : {
			name : user,
			password : psw
		},
		dataType : "JSON"
	})).result;
}

function exist(user) {
	return getJSON($.ajax({
		type : "POST",
		async : false,
		url : existURL,
		data : {
			name : user
		},
		dataType : "JSON"
	})).result;
}

function insert(user, psw, mail) {
	return getJSON($.ajax({
		type : "POST",
		async : false,
		url : registerURL,
		data : {
			name : user,
			password : psw,
			mail : mail
		},
		dataType : "JSON"
	})).result;
}

function openErrorPopUp(errors) {
	errors = "<ul>" + errors + "</ul>";
	document.getElementById("registerErrorMessage").innerHTML = errors;
	$('#registerError').popup('open');
}
